<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script>
         function getParams(){
             let url = location.search;
             let obj = null;
             if(url.indexOf("?")!=-1){
                 obj = new Object();
                 let str = url.substr(1);
                 let ps = str.split("&");
                 for(let i=0;i<ps.length;i++){
                      obj[ps[i].split("=")[0]]=decodeURIComponent(ps[i].split("=")[1]);
                 }
             }
             return obj;
         }

         $(function (){
             console.log(getParams());
         });


         function login(){
             $.ajax({
                 url:"http://localhost:9001/web/login",
                 data:$("#f").serialize(),
                 dataType:"json",
                 type:"post",
                 success:function (result){
                     if(result.status==200){
                         sessionStorage.setItem("token",result.msg);
                         window.location.href=result.data+"?token="+result.msg;
                     }else{
                         alert("登录失败!!!");
                     }
                 }
             });
         }


         //测试获取token
         function  getToken(){
             $.ajax({
                 url:"http://localhost:9001/web/getToken",
                 dataType:"json",
                 type:"post",
                 headers:{
                     "Authorization":sessionStorage.getItem("token")==null?"":sessionStorage.getItem("token")
                 },
                 success:function (result){
                    console.log(result);
                 }
             });
         }
    </script>
</head>
<body>
<button type="button" onclick="getToken()">获取</button>
<form action="javascript:void(0)" method="post" id="f">
    <label>用户名:</label><input type="text" name="username"/><br/>
    <label>密码:</label><input type="password" name="password"/><br/>
    <button type="button" onclick="login()">提交</button>
</form>
</body>
</html>